//
//  WPKindsViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/3/30.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPKindsViewController.h"
#import "WPKindsCollectionViewCell.h"
#import "WPKindsCollectionReusableView.h"


static NSString *const kUIcollectionViewCellIdentifier = @"cellIdentifier";
static NSString *const kUIcollectionReusablelIdentifier = @"reusableIdentifier";

@interface WPKindsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UIView * whiteView;/**< 白色背景 */
@property (nonatomic, strong) UICollectionView * kindCollectionView;
@property (nonatomic, strong) NSArray * kindArr;/**< 数据源 */
@property (nonatomic, strong) NSArray * keyArr;/**< 键值数组 */
@property (nonatomic, strong) NSIndexPath * currentIndexPath;/**< 当前选中item */
@property (nonatomic, assign) CGFloat height;/**< collection高度 */
- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */


@end

@implementation WPKindsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    WPKindsCollectionViewCell * cell = [self.kindCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    cell.cellTitleLabel.textColor = COLOR_RGB(61, 61, 61, 1);
}
#pragma mark - init
- (void)initializeDataSource {
    self.kindArr = @[@[@"演唱会",
                       @"话剧类",
                       @"电竞类",
                       @"舞蹈类",
                       @"音乐会"],
                     @[@"科技类",
                       @"衍生品",
                       @"家电类",
                       @"健身类",
                       @"生活类"],
                     @[@"信息技术",
                       @"文化传媒",
                       @"节能环保",
                       @"消费服务",
                       @"新材料能源",
                       @"生物制药"]];
    self.keyArr = @[@"票务众筹", @"产品众筹", @"股权众筹"];
    NSInteger count = 0;
    for (int i = 0; i < _kindArr.count; i++) {
        count += [_kindArr[i] count] /4 == 0 ?[_kindArr[i] count]/4:[_kindArr[i] count] /4+1;
    }
    self.height = (50*count+30*_keyArr.count)*DHFlexibleHorizontalMutiplier();
}
- (void)initializeUserInterface {
    self.title = @"分类";
    [self.view addSubview:self.whiteView];
    [self.view addSubview:self.kindCollectionView];
}
#pragma mark - collectionView 协议
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView  {
    return _kindArr.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_kindArr[section] count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WPKindsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kUIcollectionViewCellIdentifier forIndexPath:indexPath];
    cell.cellTitleLabel.text = _kindArr[indexPath.section][indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    WPKindsCollectionViewCell * cell = (WPKindsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (self.currentIndexPath) {
        WPKindsCollectionViewCell * cell = (WPKindsCollectionViewCell *)[collectionView cellForItemAtIndexPath:_currentIndexPath];
        cell.cellTitleLabel.textColor = COLOR_RGB(61, 61, 61, 1);
    }
    cell.cellTitleLabel.textColor = COLOR_RGB(234, 0, 34, 1);
    _currentIndexPath = indexPath;
    if (indexPath.section == 0 && indexPath.row == 0) {
        [self pushToNextController:[[NSClassFromString(@"WPVocalConvertViewController")alloc]init] withVCTitle:@"演唱会"];
    }
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10*DHFlexibleHorizontalMutiplier(), 10*DHFlexibleHorizontalMutiplier(), 10*DHFlexibleHorizontalMutiplier(), 10*DHFlexibleHorizontalMutiplier());
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    WPKindsCollectionReusableView *collectionReusableView = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        collectionReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:UICollectionElementKindSectionHeader forIndexPath:indexPath];
        collectionReusableView.titleLabel.text = _keyArr[indexPath.section];
        if (!collectionReusableView.line.superview) {
            [collectionReusableView addLine];
        }
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        collectionReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:UICollectionElementKindSectionFooter forIndexPath:indexPath];
        collectionReusableView.titleLabel.text = @"";
        collectionReusableView.backgroundColor = BGCOLOR;
    }
    return collectionReusableView;

}
#pragma mark - getter
- (UIView *)whiteView {
    if (!_whiteView) {
        _whiteView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, navBar_Fheight+10*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, _height)];
            view.backgroundColor = [UIColor whiteColor];
            view;
        });
    }
    return _whiteView;
}
- (UICollectionView *)kindCollectionView {
    if (!_kindCollectionView) {
        _kindCollectionView = ({
            UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
            //118 120
            flowLayout.itemSize = CGSizeMake(70*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
//            flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;/**< 垂直滚动 */
            flowLayout.minimumLineSpacing = 10*DHFlexibleHorizontalMutiplier();/**< 每行间距 */
            flowLayout.minimumInteritemSpacing = 20/3.0*DHFlexibleHorizontalMutiplier();/**< 设置每个Item直接的间距 */
            //配置collectionView
            //设置头部高度
            flowLayout.headerReferenceSize = CGSizeMake(CGRectGetWidth(self.view.bounds), 30*DHFlexibleHorizontalMutiplier());
            //设置尾部高度
            flowLayout.footerReferenceSize = CGSizeMake(CGRectGetWidth(self.view.bounds), 10*DHFlexibleHorizontalMutiplier());
            UICollectionView * view = [[UICollectionView alloc]initWithFrame:CGRectMake(0, navBar_Fheight+10*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, SCREEN_SIZE.height - navBar_Fheight - 10*DHFlexibleHorizontalMutiplier()- CGRectGetHeight(self.tabBarController.tabBar.frame)) collectionViewLayout:flowLayout];
            view.delegate = self;
            view.dataSource = self;
            view.backgroundColor = [UIColor clearColor];
            //[注册]
            //1.注册Cell
            [view registerClass:[WPKindsCollectionViewCell class] forCellWithReuseIdentifier:kUIcollectionViewCellIdentifier];
            //2.注册头部
            [view registerClass:[WPKindsCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:UICollectionElementKindSectionHeader];
            [view registerClass:[WPKindsCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:UICollectionElementKindSectionFooter];
            view;
        });
    }
    return _kindCollectionView;
}

@end
